package com.yeziji.constant;

import com.yeziji.common.CommonEnum;
import com.yeziji.common.CommonErrorMsg;
import com.yeziji.common.msg.DataMessageErrorMsg;
import com.yeziji.exception.ApiException;
import com.yeziji.utils.expansion.Asserts;
import com.yeziji.utils.expansion.Lists2;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 云端数据操作类型枚举
 *
 * @author hwy
 * @since 2024/10/26 20:41
 **/
@Getter
@AllArgsConstructor
public enum DataOperateTypeEnum implements CommonEnum {
    INSERT(0, "新增"),
    UPDATE(1, "更新"),
    DELETE(2, "删除"),
    ;
    private final int code;
    private final String desc;

    public static DataOperateTypeEnum getByCode(Integer code) {
        Asserts.notNull(code, CommonErrorMsg.REQUIRED_DATA_IS_NULL);

        return Lists2.filterFirstOpt(DataOperateTypeEnum.values(), statusEnum -> statusEnum.getCode() == code)
                .orElseThrow(() -> new ApiException(DataMessageErrorMsg.NOT_SUPPORTED_DB_TYPE));
    }
}
